package com.shitu.base.net;

import com.shitu.base.net.interceptor.HttpLoggingInterceptor;
import com.shitu.base.net.interceptor.ParamsInterceptor;

import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;

public class API {
    public static void init() {
        init(false);
    }


    /**
     * 网络请求封装类
     */
    public static final String DEBUG_URL = "http://52.83.146.189:88/slm/"; //测试环境
    public static final String BASE_URL = "http://52.83.146.189:88/slm/"; //正式环境
    public static final String QYPID = "02023621010000000000";
    private static Retrofit retrofit;
    private static OkHttpClient client;

    /**
     * 初始化
     *
     * @param authCookie
     * @param isDebug    使用测试环境url
     */
    public static void init(boolean isDebug) {
        client = new OkHttpClient();
        //初始化参数拦截器，添加两个通用参数
        ParamsInterceptor paramsInterceptor =
                new ParamsInterceptor.Builder()
//                        .addQueryParam("authCookie", authCookie)
//                        .addQueryParam("qypid", QYPID)
                        .build();
        //初始化log拦截器，方便跟踪调试网络请求
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);

        OkHttpClient okHttpClient = new OkHttpClient.Builder().
//                addInterceptor(paramsInterceptor).
        addInterceptor(loggingInterceptor).
                        build();

        //初始化retrofit
        String url;
        if (isDebug) {
            url = DEBUG_URL;
        } else {
            url = BASE_URL;
        }
        retrofit = new Retrofit.Builder().baseUrl(url).
                client(okHttpClient).build();
    }

    /**
     * 创建service
     *
     * @param service
     * @param <T>
     * @return
     */
    public static <T> T create(Class<T> service) {
        return retrofit.create(service);
    }

    public static void send(Request request, Callback cb) {
        client.newCall(request).enqueue(cb);
    }

}

